/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  SupervisionBusiDetailActivity.java   
 * @Package com.jiyi.activity.supervision   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-10-20 上午10:45:17   
 * @version V1.0     
 */
package com.jiyi.activity.supervision;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.supervision.SupervisionOwnerDetailBean;
import com.jiyi.bean.supervision.SupervisionOwnerDetailJson;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.SupervisionOwnerEvent;
import com.jiyi.inteface.OwnerMovePopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.OwnerMovePopView;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: SupervisionBusiDetailActivity
 * @Description:TODO(业主业务查询)
 * @author: HYM
 * @date: 2017-10-20 上午10:45:17
 * 
 */
public class SupervisionBusiDetailActivity extends ActivityBase implements OwnerMovePopInterface {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.tvOwnerName)
	TextView tvOwnerName;
	@BindView(R.id.tvTel)
	TextView tvTel;
	@BindView(R.id.tvAddress)
	TextView tvAddress;
	
	
	@BindView(R.id.tvOwnerNameDesc)
	TextView tvOwnerNameDesc;
	@BindView(R.id.tvWbName)
	TextView tvWbName;
	@BindView(R.id.tvSupervision)
	TextView tvSupervision;
	
	Dialog dialog ;
	String owner_id;
	OwnerMovePopView mOwnerMovePopView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supervision_busi_detail);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		mOwnerMovePopView.hidePopWindow();
		super.onDestroy();
	}
	
	private void init() {
		getIntentData();
		setTop();
		getData();
		mOwnerMovePopView = new OwnerMovePopView(this, this);
	}

	private void getIntentData() {
		owner_id = getIntent().getStringExtra("owner_id");
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", owner_id);
		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this,
				WebApi.SUPERVISION_BUSI_OWNER_DETAIL, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								SupervisionOwnerDetailBean temp = new Gson()
										.fromJson(
												data,
												SupervisionOwnerDetailBean.class);
								tvOwnerName.setText(temp.getCompany_name());
								tvTel.setText(temp.getPhone());
								tvAddress.setText(temp.getAddress());
								tvOwnerNameDesc.setText(temp.getCompany_name());
								tvWbName.setText(temp.getBranch_name());
								tvSupervision.setText(TextUtils.isEmpty(temp.getUser_name()) ? temp.getSupervise_name():temp.getUser_name());
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	private void setTop() {
		tvTitle.setText("业务查询");
		if (UserInfoUtil.getCurrentUserSupervisionRole() == 3) {
			tvSearch.setVisibility(View.GONE);
		}
	}
	
	@OnClick({ R.id.tvBack,R.id.tvSearch })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			getOwnerDetail(owner_id,1);
			break;
		default:
			break;
		}
	}
	
	/**   
	 * @author: HYM 
	 * @Title: getHuoDongDetail   
	 * @Description: TODO(获取业主详情)   

	 * @date: 2017-7-17
	 */  
	void getOwnerDetail(final String ownerId,final int position)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", ownerId);
		params.put("jg_role_id", UserInfoUtil.getCurrentUserSupervisionRole()+"");

		dialog = showDialog(dialog);
		
		VolleyRequest.StringRequestPost(this, WebApi.SUPERVISION_OWNER_MOVE_DETAIL, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								SupervisionOwnerDetailJson temp = new Gson().fromJson(
										result, SupervisionOwnerDetailJson.class);
								
								mOwnerMovePopView.showPopWindow(temp.getData(),ownerId,position);
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
	
	@Override
	public void OnSureListener(String supervision_id,String ownerId,int position) {
		doPost(supervision_id, ownerId, position);
	}
	
	/**   
	 * @author: HYM 
	 * @Title: doPost   
	 * @Description: TODO(转移)   
	 * @param supervised_id 接收方的监管id
	 * @param id 当前业主id
	 * @param position void 
	 * @date: 2017-10-19
	 */  
	void doPost(String supervised_id,final String id,final int  position)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", id);
		params.put("receiver_id", supervised_id);

		dialog = showDialog(dialog);
		
		VolleyRequest.StringRequestPost(this, WebApi.SUPERVISION_OWNER_MOVE_SUBMIT, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								EventBus.getDefault().post(new SupervisionOwnerEvent(1,id));
								ToastUtil.getInstances().showMsg(info);
								mOwnerMovePopView.hidePopWindow();
								finish();
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
}
